package main

import (
	"fmt"
	"runtime"
	"sync"
	"sync/atomic"
)

//协成等待结束
//且使用原子计数器
func main() {
	var num uint64
	var wg sync.WaitGroup
	for i := 0; i < 1000; i++ {
		wg.Add(1)
		go func(i uint64) {
			atomic.AddUint64(&num, i)
			runtime.Gosched()
			wg.Done()
		}(uint64(i))
	}
	wg.Wait()
	numFinal := atomic.LoadUint64(&num)
	fmt.Println(numFinal)
}
